package com.utility;

import java.util.Random;

import org.apache.commons.configuration.Configuration;

/*	
 * Functions:
 * public String IDGenerator ();				generated ID with default(8) digits.	[a-zA-Z0-9]
 * public String IDGenerator (int digit);		generated ID with given digits.			[a-zA-Z0-9]
 * 
 */

public class Utility {
	
	Configuration configuration = PropertiesConjunction.getCoreConfiguration();
	
	final int DEFAULT_DIGIT = configuration.getInt("com.utility.idgen.defaultlength", 10);
	
	
	public String IDGenerator() {
		return IDGenerator(DEFAULT_DIGIT);
		
	}
	
	public String IDGenerator(int digit) {
		
		String validset = configuration.getString("com.utility.idgen.character", "ABCDEFG");
		
		char [] validsetChar = validset.toCharArray();
		int length = validset.length();
		Random random = new Random();
		StringBuffer buff = new StringBuffer();
		
		while (digit > 0) {
			buff.append(validsetChar[random.nextInt(length)]);
			digit--;
			
		}
		
		return buff.toString();
		
	}
	
	public void setProperty(Configuration configuration) {
		this.configuration = configuration;
	}
	
}
